var trades = [];
var apiready = function() {

    /* 一个获取导航栏高度的方法. */
    window.getNavHeight = function() {
        var h = 44;

        if ("ios" == api.systemType && parseInt(api.systemVersion) >= 7) {
            h = 64;
        }

        return h;
    }

    /* 一个关闭窗口,返回上一级页面的优雅实现. */
    window.closeWin = function(winName) {
        api.closeWin();
    }

    // -----------------------------------------
    /* 导航栏自适应iOS 7. */
    var header = $api.byId('header');
    $api.fixIos7Bar(header);

    /* Android,点击返回键,可以返回上一级. */
    window.addEventListener("keyback", function() {
        window.addEventListener("keyback", function() {
            if (!api["pageParam"]["isFrame"]) { // 仅在是主窗口时,才捕捉 keyback事件.
                window.closeWin();
            }
        });
    });

	getTrades(function(result, err) {
		if (result) {
			trades = result.values;
			renderExpandList();
		} else {
			api.alert({
				msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
			});
		};
	});
};

var getTrades = function(callback) {
	api.ajax({
		url: SERVER_URL + '/entity/config/trades',
		method: 'get',
		timeout: 30,
		dataType: 'json',
		returnAll: false,
		data: {}
	}, callback);
};

var renderExpandList = function() {
	var tradesName = api["pageParam"]["name"];
	var $main = $api.dom('#main');
	var $nowTrades = $api.dom('#now_trades');
	trades.some(function(item, i) {
		if (item.name == tradesName) {
			$nowTrades.innerText = item.name;

			var priceArr = [];
			item.price.some(function(priceItem, j) {
				priceArr.push('<div class="row inwrap">');
				priceArr.push('<div class="col flipcard" onclick="openWaiters(\'' + item.name + '\', \'' + priceItem.name + '\')">');
				priceArr.push(priceItem.name);
				priceArr.push(priceItem.money + '$/' + priceItem.time + 'min');
				priceArr.push('</div>');
				priceArr.push('</div>');
			});

			$api.after($nowTrades, priceArr.join(''));
		}

		var tradesArr = [];
		tradesArr.push('<div class="classify" tapmode onclick="expandList(\'price_' + item.name + '\')">');
		tradesArr.push('<img src="' + SERVER_URL + '/entity/file/' + item.icon + '" alt="" class="sublogo">');
		tradesArr.push('<div class="info">');
		tradesArr.push('<div class="title">' + item.name + '</div>');
		tradesArr.push('</div>');
		tradesArr.push('<img src="../image/ic_arrow_down_black.png" alt="" class="arrow" id="price_' + item.name + 'arrow">');
		tradesArr.push('</div>');

		$api.append($main, tradesArr.join(''));

		var priceHtml = '<div class="subclass" id="price_' + item.name + '" data-click="0">';
		item.price.some(function(priceItem, j) {
			var priceArr = [];
			priceArr.push('<div class="row">');
			priceArr.push('<div class="col flipcard bb" onclick="openWaiters(\'' + item.name + '\', \'' + priceItem.name + '\')">');
			priceArr.push(priceItem.name);
			priceArr.push(priceItem.money + '$/' + priceItem.time + 'min');
			priceArr.push('</div>');
			priceArr.push('</div>');
			priceHtml += priceArr.join('');
		});
		priceHtml += '</div>';

		$api.append($main, priceHtml);
	});
};

var expandList = function(type) {
	var food = document.getElementById(type);
	var click = food.getAttribute("data-click");

	if (click == 0) {
		// 点开
		food.setAttribute("data-click", 1);
		$api.dom('#' + type + 'arrow').src = "../image/ic_arrow_up_black.png";
		food.style.display = 'block';
	} else {
		// 关闭
		food.setAttribute("data-click", 0);
		$api.dom('#' + type + 'arrow').src = "../image/ic_arrow_down_black.png";
		food.style.display = 'none';
	}
}

var openWaiters = function(trades, price) {

	var delay = 0;

	if ("ios" != api.systemType) {
		delay = 300;
	}

	api.openWin({
		name: "waiters",
		url: "./waiters.html",
		pageParam: {
			trades: trades,
			price: price
		},
		bounces: false,
		vScrollBarEnabled: false,
		hScrollBarEnabled: false,
		reload: true,
		delay: delay
	});

};
